package com.cgc.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cgc.service.BaseService;
import com.cgc.utils.ReturnJson;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction<T> extends ActionSupport
		implements ServletRequestAware, ServletResponseAware, ServletContextAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BaseService<T> baseService;

	private HttpServletRequest request;

	private HttpServletResponse response;

	private ServletContext application;

	private int page; // 页码

	private int rows;// 每一页多少条记录
	
	private int id;
	
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public BaseService<T> getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService<T> baseService) {
		this.baseService = baseService;
	}

	private T t;
	
	private T t1;

	public T getT() {
		return t;
	}

	public void setT(T t) {
		this.t = t;
	}
	
	

	public T getT1() {
		return t1;
	}

	public void setT1(T t1) {
		this.t1 = t1;
	}

	/**
	 * 添加
	 */
	public void add() {
		System.out.println(".." + t);
		// 返回给前端 {"success":true,"message":""}
		Map<String, Object> rtn = new HashMap<String, Object>();
		try {
			// 调用业务service的方法
			baseService.add(t);
			rtn.put("success", true);
			rtn.put("message", "新增成功");
		} catch (Exception e) {
			e.printStackTrace();
			rtn.put("success", false);
			rtn.put("message", "新增失败");
		}
		ReturnJson.writejson(JSON.toJSONString(rtn), response);
	}

	/***
	 * 查询列表
	 */
	public void list() {
		List<T> list =baseService.list(t1);
		System.out.println(JSON.toJSONString(list));
		ReturnJson.writejson(JSON.toJSONString(list), response);
	}

	/**
	 * 删除
	 */
	public void delete() {
		Map<String,Object> mapData = new HashMap<String,Object>();
		try {
			baseService.delete(id);
			mapData.put("code", "0");
			mapData.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			mapData.put("code", "1");
			mapData.put("message", "删除失败");
		}
		
		ReturnJson.writejson(JSON.toJSONString(mapData), response);

	}

	/**
	 * 分页查询
	 */
	public void listByPage() {
		int firstResult = (page - 1) * rows;
		System.out.println("页码"+page);
		System.out.println("每一页多少条记录"+rows);
		List<T> list = baseService.getListByPage(t1,firstResult, rows);
		// 总记录条数
		long total = baseService.getCount(t1);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", total);
		mapData.put("rows", list);
		String listString = JSON.toJSONString(mapData, SerializerFeature.WriteMapNullValue,
				SerializerFeature.DisableCircularReferenceDetect);
		ReturnJson.writejson(listString, response);
	}

	/**
	 * 通过编辑查询对象
	 */
	public void get() {
		T t =baseService.get(id);
		String jsonString =JSON.toJSONStringWithDateFormat(t, "yyyy-MM-dd");
		System.out.println(jsonString);
		String jsonStringAfter = mapData(jsonString,"t");
		System.out.println("转换后的json:"+jsonStringAfter);
		ReturnJson.writejson(jsonStringAfter, response);
		
	}

	/*
	 * 更新
	 */
	public void update() {
		
		Map<String,Object> mapData = new HashMap<String,Object>();
		try {
			baseService.update(t);
			mapData.put("code", "0");
			mapData.put("message", "更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			mapData.put("code", "1");
			mapData.put("message", "更新失败");
		}
		
		ReturnJson.writejson(JSON.toJSONString(mapData), response);
		
	}

	@Override
	public void setServletContext(ServletContext application) {
		this.application = application;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public ServletContext getApplication() {
		return application;
	}

	public void setApplication(ServletContext application) {
		this.application = application;
	}
	
	/**
	 * 
	 * @param jsonString
	 * @param prefix
	 * @return  {"t.d_anme":"技术术部","t.d_address":"北京","t.d_id":1}
	 */
	public String mapData(String jsonString,String prefix){
		Map<String,Object> map = JSON.parseObject(jsonString);
		
		Map<String,Object> dataMap = new  HashMap<String, Object>();
		
		for(String key:map.keySet()){
			if(map.get(key) instanceof Map){
				//key值进行拼接
				Map<String,Object> m2 = (Map<String, Object>) map.get(key);
				for(String key2 :m2.keySet()){
					dataMap.put(prefix+"."+key+"."+key2, m2.get(key2));
				}
			}else{
				dataMap.put(prefix+"."+key, map.get(key));
			}
			
		}
		return JSON.toJSONString(dataMap);
	}

}
